﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Linq;
using UniMana.Utils.Data;

namespace UniMana.Win.Training.xucUserControl.xucPersonel
{
    public partial class xucWeek : DevExpress.XtraEditors.XtraUserControl
    {
        private Bitmap m_bitmap = Properties.Resources.schedule;
        private dynamic m_dnmDataSource;
        private DataTable m_dt;

        private string m_strFromDay;
        private string m_strToDay;

        private string[] m_strSession = new string[3];
        private string[] m_strSessionFrom = new string[3];
        private string[] m_strSessionTo = new string[3];

        private string[] m_strClassID = new string[21];
        private string[] m_strSubjectName = new string[21];
        private string[] m_strCredits = new string[21];
        private string[] m_strRoom = new string[21]; 
        public xucWeek()
        {
            InitializeComponent();
        }
        private void DrawString(string _strText, int _iPx, int _iPy, bool bold = false, float size = 11.0f)
        {
            Font font;
            if (bold)
            {
                font = new Font("Tahoma", size, FontStyle.Bold);
            }
            else
            {
                font = new Font("Tahoma", size);
            }
            Graphics grp = Graphics.FromImage(this.m_bitmap);
            grp.DrawString(_strText, font, Brushes.Black, new Point(_iPx, _iPy));
            grp.Flush();
            grp.Dispose();
        }
        public string[] SessionName
        {
            set
            {
                for (int i = 0; i < value.Length; i++)
                {
                    int iPx = 0;
                    int iPy = 0;
                    switch (i)
                    {
                        case 0:
                            iPx = 122;
                            iPy = 39;
                            break;
                        case 1:
                            iPx = 122;
                            iPy = 119;
                            break;
                        case 2:
                            iPx = 122;
                            iPy = 199;
                            break;
                    }
                    DrawString(value[i], iPx, iPy);
                }
            }
        }
        public string[] SessionFromTime
        {
            get
            {
                return this.m_strSessionFrom;
            }
            set
            {
                this.m_strSessionFrom = value;
                for (int i = 0; i < value.Length; i++)
                {
                    int iPx = 0;
                    int iPy = 0;
                    switch (i)
                    {
                        case 0:
                            iPx = 122;
                            iPy = 51;
                            break;
                        case 1:
                            iPx = 122;
                            iPy = 131;
                            break;
                        case 2:
                            iPx = 122;
                            iPy = 211;
                            break;
                    }
                    DrawString(value[i], iPx, iPy);
                }
            }
        }
        public string[] SessionToTime
        {
            get
            {
                return this.m_strSessionTo;
            }
            set
            {
                this.m_strSessionTo = value;
                for (int i = 0; i < value.Length; i++)
                {
                    int iPx = 0;
                    int iPy = 0;
                    switch (i)
                    {
                        case 0:
                            iPx = 122;
                            iPy = 63;
                            break;
                        case 1:
                            iPx = 122;
                            iPy = 143;
                            break;
                        case 2:
                            iPx = 122;
                            iPy = 223;
                            break;
                    }
                    DrawString(value[i], iPx, iPy);
                }
            }
        }
        public string[] ClassID
        {
            get
            {
                return this.m_strClassID;
            }
            set
            {
                this.m_strClassID = value;
                for (int i = 0; i < value.Length; i++)
                {
                    if (i >= 21)
                        break;
                    if (value[i] != null)
                    {
                        if (value[i] != "")
                        {
                            int iPx = (i % 7) * 150 + 200;
                            int iPy = (i / 7) * 80 + 25;
                            DrawString("Mã lớp: ", iPx, iPy, true);
                            DrawString(value[i], iPx + 50, iPy);
                        }
                    }
                }
            }
        }
        public string[] SubjectName
        {
            get
            {
                return this.m_strSubjectName;
            }
            set
            {
                this.m_strSubjectName = value;
                for (int i = 0; i < value.Length; i++)
                {
                    if (i >= 21)
                        break;
                    if (value[i] != null)
                    {
                        if (value[i] != "")
                        {
                            int iPx = (i % 7) * 150 + 200;
                            int iPy = (i / 7) * 80 + 37;
                            if (value[i][0] == ' ')
                            {
                                value[i] = value[i].Substring(1);
                            }
                            if (value[i][value[i].Length - 1] == ' ')
                            {
                                value[i] = value[i].Substring(0, value[i].Length - 1);
                            }
                            DrawString("Tên môn: ", iPx, iPy, true);
                            string[] split = value[i].Split(' ');
                            if (split.Length >= 4)
                            {
                                int mid = 2;
                                string result = " " + split[0];
                                bool firstrow = false;
                                for (int j = 1; j < split.Length; j++)
                                {
                                    if (split[j] != "")
                                    {
                                        if (j > 0 && j % mid == 0 && j != 4)
                                        {
                                            if (!firstrow)
                                            {
                                                DrawString(result, iPx + 55, iPy);
                                                mid = 4;
                                            }
                                            else
                                            {
                                                DrawString(result, iPx + 5, iPy);
                                            }
                                            iPy += 12;
                                            result = "";
                                            firstrow = true;
                                        }
                                        result += " " + split[j];
                                    }
                                }
                                if (result != "")
                                {
                                    if (!firstrow)
                                    {
                                        DrawString(result, iPx + 55, iPy);
                                    }
                                    else
                                    {
                                        DrawString(result, iPx + 5, iPy);
                                    }
                                }
                            }
                            else
                            {
                                DrawString(value[i], iPx + 55, iPy);
                            }
                        }
                    }
                }
            }
        }
        public string[] Credits
        {
            get
            {
                return this.m_strCredits;
            }
            set
            {
                this.m_strCredits = value;
                for (int i = 0; i < value.Length; i++)
                {
                    if (i >= 21)
                        break;
                    if (value[i] != null)
                    {
                        if (value[i] != "")
                        {
                            int iPx = (i % 7) * 150 + 200;
                            int iPy = (i / 7) * 80 + 78;
                            DrawString("Số TC: ", iPx, iPy, true);
                            DrawString(value[i], iPx + 40, iPy);
                        }
                    }
                }
            }
        }
        public string[] Room
        {
            get
            {
                return this.m_strRoom;
            }
            set
            {
                this.m_strRoom = value;
                for (int i = 0; i < value.Length; i++)
                {
                    if (i >= 21)
                        break;
                    if (value[i] != null)
                    {
                        if (value[i] != "")
                        {
                            int iPx = (i % 7) * 150 + 260;
                            int iPy = (i / 7) * 80 + 78;
                            DrawString("Phòng: ", iPx, iPy, true);
                            DrawString(value[i], iPx + 45, iPy);
                        }
                    }
                }
            }
        }

        public string FromDay
        {
            get { return this.m_strFromDay; }
            set
            {
                this.m_strFromDay = value;
                DrawString(value, 10, 90, size: 12);
            }
        }
        public string ToDay
        {
            get
            {
                return this.m_strToDay;
            }
            set
            {
                this.m_strToDay = value;
                DrawString(value, 10, 155, size: 12);
            }
        }
        public Image Image
        {
            get { return this.m_bitmap; }
        }
        public dynamic DataSource
        {
            get
            {
                return this.m_dnmDataSource;
            }
            set
            {
                this.m_dnmDataSource = value;
                if (value is IQueryable)
                {
                    this.m_dt = ((IQueryable)value).ToDataTable();
                }
                else
                {
                    this.m_dt = value;
                }
                if (this.m_dt != null)
                {
                    if (this.m_dt.Rows.Count == 16)
                    {
                       
                        
                        string strTimeFromDay = this.m_dt.Rows[2][0].ToString();
                        string strTimeToDay = this.m_dt.Rows[4][0].ToString();

                        string strSession1FromTime = this.m_dt.Rows[2][1].ToString();
                        string strSession1ToTime = this.m_dt.Rows[3][1].ToString();
                        string strSession2FromTime = this.m_dt.Rows[7][1].ToString();
                        string strSession2ToTime = this.m_dt.Rows[8][1].ToString();
                        string strSession3FromTime = this.m_dt.Rows[12][1].ToString();
                        string strSession3ToTime = this.m_dt.Rows[13][1].ToString();

                        string[] strSession = new string[] { "1", "2", "3" };
                        string[] strSessionFrom = new string[] { strSession1FromTime, strSession2FromTime, strSession3FromTime };
                        string[] strSessionTo = new string[] { strSession1ToTime, strSession2ToTime, strSession3ToTime };
                        string[] strClassID = new string[21];
                        string[] strSubjectName = new string[21];
                        string[] strCredits = new string[21];
                        string[] strRoom = new string[21];

                        int count = 0;
                        for (int i = 1; i < 16; i += 5)
                        {
                            for (int j = 2; j < 9; j++)
                            {
                                string classID = this.m_dt.Rows[i][j].ToString();
                                if (classID != "")
                                {
                                    string subjectName = this.m_dt.Rows[i + 1][j].ToString();
                                    string credits = this.m_dt.Rows[i + 2][j].ToString();
                                    //string teacherName = this.m_dt.Rows[i + 3][j].ToString();
                                    string Room = this.m_dt.Rows[i + 4][j].ToString();

                                    strClassID[count] = classID;
                                    strSubjectName[count] = subjectName;
                                    strCredits[count] = credits;
                                    strRoom[count] = Room;
                                }
                                count++;
                            }
                        }
                        this.SessionName = strSession;
                        this.SessionFromTime = strSessionFrom;
                        this.SessionToTime = strSessionTo;
                        this.ClassID = strClassID;
                        this.Credits = strCredits;
                        this.Room = strRoom;
                        this.SubjectName = strSubjectName;
                        this.FromDay = strTimeFromDay;
                        this.ToDay = strTimeToDay;
                        this.BackgroundImage = this.m_bitmap;
                    }
                }
            }
        }
    }
}
